package com.timemanage.utils

import android.app.Notification
import android.content.BroadcastReceiver
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import androidx.core.app.NotificationCompat
import com.timemanage.utils.AlarmService
import com.timemanage.R
import com.timemanage.utils.AlarmReceiver

class AlarmReceiver : BroadcastReceiver() {
    private var manager: NotificationManager? = null
    private var title: String? = null
    private var content: String? = "提醒的时间到啦，快看看你要做的事..."



    override fun onReceive(context: Context, intent: Intent) {

//此处接收闹钟时间发送过来的广播信息，为了方便设置提醒内容
        title = intent.getStringExtra("title")
        content = intent.getStringExtra("content ")
        showNormal(context)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        intent.setClass(context, AlarmService::class.java)
        context.startService(intent) //回调Service,同一个Service只会启动一个，所以直接再次启动Service，会重置开启新的提醒，
    }

    /**     * 发送通知      */
   fun showNormal(context: Context) {

        manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val notification = NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.caution) //设置通知图标。
            .setTicker(content) //通知时在状态栏显示的通知内容
            .setContentInfo("便签提醒") //内容信息
            .setContentTitle(title) //设置通知标题。
            .setContentText(content) //设置通知内容。
            .setAutoCancel(true) //点击通知后通知消失
            .setDefaults(Notification.DEFAULT_ALL) //设置系统默认的通知音乐、振动、LED等。
            .build()
        manager!!.notify(NOTIFICATION_ID_1, notification)
    }

    companion object {
        private const val NOTIFICATION_ID_1 = 0x00113
    }
}